<?php
require_once '../sub/init.php';
require_once 'chkUadm.php';
require_once ZEAI.'cache/config_up.php';
require_once ZEAI.'sub/zeai_up_func.php';

if(!in_array('trend',$QXARR) || !in_array('qun',$QXARR))exit(noauth());
if (!ifint($classid) && $submitok != 'ajax_pic_path_s_up' && $submitok != "add_update" && $submitok != "add")callmsg("forbidden","-1");

if ($submitok == "add_update" || $submitok == "mod_update") {
	if(!ifint($uid))json_exit(array('flag'=>0,'msg'=>'uid不能为空'));
	if(empty($content))json_exit(array('flag'=>0,'msg'=>'请输入【内容】'));
	if(str_len($content)>1000)json_exit(array('flag'=>0,'msg'=>'【内容】请小于1000字节'));
	$content = dataIO($content,'in');
	if(ifint($uid)){
		$row = $db->ROW(__TBL_USER__,"id","id=".$uid);
		if (!$row)json_exit(array('flag'=>0,'msg'=>'此用户不存在'));
	}
}

if ($submitok == "add_update") {
	if(!empty($pathlist)){
		$ARR=explode(',',$pathlist);
		if (count($ARR) >= 1 && is_array($ARR)){
			$pathlist=array();
			foreach ($ARR as $V) {
				adm_pic_reTmpDir_send($V,'qun');
				adm_pic_reTmpDir_send(smb($V,'b'),'qun');
				$path_s = str_replace('tmp','qun',$V);
				$pathlist[]=$path_s;
			}
			$piclist = implode(',',$pathlist);
		}
	}
	if(ifint($form_qid)){
		$db->query("INSERT INTO ".__TBL_TREND__." (uid,piclist,content,addtime,kind,qid) VALUES ($uid,'$piclist','$content',".ADDTIME.",2,$form_qid)");
		AddLog('【单身群】话题发布:');
	}else{
		$db->query("INSERT INTO ".__TBL_TREND__." (uid,piclist,content,addtime) VALUES ($uid,'$piclist','$content',".ADDTIME.")");
		AddLog('【动态】话题发布');
	}
	json_exit(array('flag'=>1,'msg'=>'发布成功'));

}elseif($submitok == 'mod_update'){
	$row = $db->ROW(__TBL_TREND__,"piclist","id=".$classid,"num");
	if ($row){
		$data_pathlist= $row[0];
	}else{
		alert_adm("记录不存在","back");
	}
	$pathlist = tmp_piclist_modupdate($pathlist,$data_pathlist,'qun','list');
	$SQL = ",piclist='$pathlist'";
	if(ifint($form_qid))$SQL .= ",qid='$form_qid'";
	$db->query("UPDATE ".__TBL_TREND__." SET content='$content'".$SQL." WHERE id='$classid'");
	AddLog('【动态/单身群】话题内容修改->话题id:'.$classid);
	json_exit(array('flag'=>1,'msg'=>'操作成功'));
}elseif($submitok == 'ajax_pic_path_s_up'){
	if (ifpostpic($file['tmp_name'])){
		$dbname = setphotodbname('tmp',$file['tmp_name'],'');
		if (!up_send($file,$dbname,0,$_UP['upMsize'],$_UP['upBsize']))json_exit(array('flag'=>0,'msg'=>'图片写入失败'));
		$dbname=setpath_s($dbname);
		$newpic = $_ZEAI['up2']."/".$dbname;
		if (!ifpic($newpic))json_exit(array('flag'=>0,'msg'=>'图片格式错误'));
		json_exit(array('flag'=>1,'msg'=>'上传成功','dbname'=>$dbname));
	}else{
		json_exit(array('flag'=>0,'msg'=>'zeai_tmp_name_error'));
	}
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<style>.picli60 li{margin:10px 30px 10px 0;margin-bottom:10px}</style>
</head>
<body class="body">
<?php 
if($submitok=='mod'){
	$rt = $db->query("SELECT content,piclist,qid,uid,kind FROM ".__TBL_TREND__." WHERE id=".$classid);
	if($db->num_rows($rt)) {
		$row = $db->fetch_array($rt);
		$piclist = $row['piclist'];
		$content = dataIO($row['content'],'out');
		$uid = $row['uid'];
		$qid = $row['qid'];
		$kind = $row['kind'];
	}
}else{
	if(ifint($qid)){
		$row = $db->ROW(__TBL_QUN__,"uid","id=".$qid,"num");
		if ($row){
			$uid= $row[0];
		}
	}
}
?>
<form id="Www_zeai_cn_form">
<table class="table W90_ size2" style="margin:10px auto 50px auto">
<?php if($kind==2){?>
<tr>
    <td class="tdL">所属单身群</td>
    <td class="tdR">
	  <?php
        $rt2=$db->query("SELECT id,title FROM ".__TBL_QUN__." WHERE flag=1 ORDER BY px DESC,id DESC");
        $total2 = $db->num_rows($rt2);
        if ($total2 > 0) {?>
            <select name="form_qid" class="size2">
            <?php
            for($j=0;$j<$total2;$j++) {
                $rows2 = $db->fetch_array($rt2,'num');
                if(!$rows2) break;
                $clss=($qid==$rows2[0])?' selected':'';?><option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[1],'out');?></option><?php
            }?>
            </select><?php
        }?>
    </td>
</tr>
<?php }?>

<tr>
    <td class="tdL">用户UID</td>
    <td class="tdR"><input name="uid" class="W100" type="text" value="<?php echo $uid;?>" /></td>
</tr>
<tr>
  <td class="tdL">图片</td>
  <td class="tdR">
    <div class="picli60" id="picli_pathlist">
        <li class="add" id="pathlist_add"></li>
        <?php
        if(!empty($piclist)){
            $ARR=explode(',',$piclist);
            foreach ($ARR as $V) {
               echo '<li><img src="'.$_ZEAI['up2'].'/'.$V.'"><i></i></li>';
            }
        }?>      
	</div>
  </td>
</tr>
<tr>
<td class="tdL">内容</td>
<td class="tdR">
<textarea name="content" id="content" rows="5" id=textarea class="W100_ S14"><?php echo $content;?></textarea>
<input name="pathlist" id="pathlist" type="hidden" value="" />
<?php if ($submitok == 'mod'){?>
  <input name="submitok" type="hidden" value="mod_update" />
  <input name="classid" type="hidden" value="<?php echo $classid;?>" />
<?php }else{ ?>
  <input name="submitok" type="hidden" value="add_update" />
<?php }?>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="提交" type="button" id="submit_add" class="btn size3" value="　保存　"></td>
</tr>
</table>
</form>
<script>
	var up2='<?php echo $_ZEAI['up2'];?>/',upMaxMB=<?php echo $_UP['upMaxMB']; ?>,classid=<?php echo intval($classid); ?>;
    submit_add.onclick=function(){
        if (zeai.empty(o('content').value)){
            zeai.msg('请输入【内容】'+o('content').value,o('content'));	
            return false;
        }
		zeai.ajax({url:'trend_mod'+zeai.extname,form:Www_zeai_cn_form},function(e){var rs=zeai.jsoneval(e);
			zeai.msg(0);
			if(rs.flag==1){
				zeai.msg(rs.msg);
				setTimeout(function(){parent.location.reload(true);},1000);
			}else{
				zeai.msg(rs.msg);
			}
		});
    }
	//
	<?php if($submitok=='mod'){?>
	window.onload=function(){end();}
	<?php }?>
	zeai.photoUp({
		btnobj:pathlist_add,
		upMaxMB:upMaxMB,
		url:"trend_mod.php?classid="+classid,
		multiple:8,
		submitok:"ajax_pic_path_s_up",
		end:function(rs){end();},
		li:function(rs){
			zeai.msg(0);zeai.msg(rs.msg);
			if (rs.flag == 1){picli_pathlist.append('<li><img src="'+up2+rs.dbname+'"><i></i></li>');}
		}
	});
	function end(){
		var i=zeai.tag(picli_pathlist,'i'),img=zeai.tag(picli_pathlist,'img');
		if(zeai.empty(i))return;
		for(var k=0;k<img.length;k++) {
			(function(k){
				var src=img[k].src;
				img[k].onclick = function(){parent.piczoom(src.replace('_s.','_b.'));}
			})(k);
		}
		for(var k=0;k<i.length;k++) {
			(function(k){
				i[k].onclick = function(){
					var thiss=this;
					zeai.confirm('亲~~确认删除么？',function(){
						thiss.parentNode.remove();
						pathlistReset();
					});
				}
			})(k);
		}
		function pathlistReset(){
			var img=zeai.tag(picli_pathlist,'img'),pathlist=[],src;
			for(var k=0;k<img.length;k++){
				var src=img[k].src.replace(up2,'');
				pathlist.push(src);
			}
			o('pathlist').value=pathlist.join(",");
		}
		pathlistReset();
	}
    </script>
</body></html>